package com.example.caoh.common.util;

import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;

import com.alibaba.fastjson.JSONObject;
/**
 * @author wuxi
 */
public class ElsRequest {
	
	
	
	/**
	 * 新增文档
	 * @param url
	 * @param index
	 * @param type
	 * @param id
	 * @param json
	 * @throws Exception
	 */
	public static String put(String url, String index_and_type , String type, String id, JSONObject data) throws Exception {
		return Request.Put(String.format("%s/%s/%s", url, index_and_type,  id))
				.bodyString(data.toString(), ContentType.APPLICATION_JSON).execute().returnContent().asString();
	}
	
	public static String bulk(String url,String data) throws Exception {
		return Request.Post(String.format("%s/_bulk", url))
				.bodyString(data, ContentType.APPLICATION_JSON).execute().returnContent().asString();
	}
	
	/**
	 * 创建索引
	 * @param url
	 * @param index
	 * @param mapping
	 * @return
	 * @throws Exception
	 */
	public static String create(String url,String index,String mapping) throws Exception {
		return Request.Put(String.format("%s/%s", url,index))
				.bodyString(mapping, ContentType.APPLICATION_JSON).execute().returnContent().asString();
	}
	/**
	 * 更新文档
	 * @param url
	 * @param index
	 * @param type
	 * @param id
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String save(String url, String index_and_type, String id, JSONObject data) throws Exception {
		JSONObject obj = new JSONObject();
		obj.put("doc", data);
		return Request.Post(String.format("%s/%s/%s/_update", url, index_and_type, id))
				.bodyString(obj.toString(), ContentType.APPLICATION_JSON).execute().returnContent().asString();
	}
	/**
	 * 根据ID查找文档
	 * @param url
	 * @param index
	 * @param type
	 * @param id
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String get(String url, ElsIndex index, String id) throws Exception {
		return Request.Get(String.format("%s/%s/%s/_source", url, index, id)).execute().returnContent()
				.asString();
	}
	
	/**
	 * @param url
	 * @param index
	 * @param type
	 * @param search
	 * @return
	 * @throws Exception
	 */
	public static String search(String url,  ElsIndex index, String jsonData) throws Exception {
		
		return Request.Post(String.format("%s/%s/_search", url, index))
				.bodyString(jsonData, ContentType.APPLICATION_JSON).execute().returnContent()
				.asString();
	}
	
	/**
	 * 删除文档
	 * @param url
	 * @param index
	 * @param type
	 * @param id
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String delete(String url, String index_and_type, String id) throws Exception {
		return Request.Delete(String.format("%s/%s/%s", url, index_and_type, id)).execute().returnContent().asString();
	}
	
	/**
	 * 判断文档是否存在
	 * @param url
	 * @param index
	 * @param type
	 * @param id
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static boolean exist(String url, String index_and_type, String id) throws Exception {
		int status = Request.Head(String.format("%s/%s/%s", url, index_and_type, id)).execute().returnResponse()
				.getStatusLine().getStatusCode();
		return status == 200;
	}

	
}
